﻿using System.IO;
using System.Web.Hosting;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using visinia.SmartEngine.Site;

namespace visinia.SmartEngine
{    
    public class WebPageController
    {
        #region Web Page Title
        public static void AddTitle(Page webpage, string title)
        {
            if (!IsValid(webpage))
                return;   
            webpage.Title = title;
        }

        public static void AppendTitle(Page webpage, string title)
        {
            if (!IsValid(webpage))
                return;   
            if (string.IsNullOrEmpty(webpage.Title))
                webpage.Title = title;
            else
                webpage.Title += " | " + title;
        } 
        #endregion

        #region Meta Tags
        public enum MetaTag
        {
            /// <summary>
            /// The web page description meta tag
            /// </summary>
            Description,
            /// <summary>
            /// The author of the contents
            /// </summary>
            Author,
            /// <summary>
            /// The web page keywords
            /// </summary>
            Keywords
        }

        public static void AddMetaTag(Page webpage, MetaTag tag, string value)
        {
            AddMetaTagInternal(webpage, tag.ToString(), value);
        }

        public static void AddMetaTag(Page webpage, string name, string value)
        {
            AddMetaTagInternal(webpage, name, value);
        }

        private static void AddMetaTagInternal(Page webpage, string name, string value)
        {
            if (!IsValid(webpage))
                return;   
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(value))
                return;            
            HtmlMeta meta = new HtmlMeta();
            meta.Name = name;
            meta.Content = webpage.Server.HtmlEncode(value);
            foreach (Control control in webpage.Header.Controls)
            {
                if (control is HtmlMeta)
                {
                    HtmlMeta tempMeta = (HtmlMeta)control;
                    if (string.Compare(tempMeta.Name, meta.Name, true) == 0)
                    {
                        webpage.Header.Controls.Remove(control);
                        break;
                    }
                }
            }
            webpage.Header.Controls.Add(meta);
        } 
        #endregion

        #region Web Page Head Links
        public static void AddHeadLink(Page webpage, string title, string link, string rel)
        {
            if (!IsValid(webpage))
                return;   
            if (string.IsNullOrEmpty(link))
                return;
            HtmlLink headLink = new HtmlLink();
            headLink.Attributes["rel"] = rel;
            headLink.Attributes["title"] = title;
            headLink.Attributes["href"] = link;
            foreach (Control control in webpage.Header.Controls)
            {
                if (control is HtmlLink)
                {
                    HtmlLink temp = (HtmlLink)control;
                    if ((string.Compare(temp.Attributes["rel"], rel, true) == 0) &&
                        (string.Compare(temp.Attributes["title"], title, true) == 0) &&
                        (string.Compare(temp.Attributes["href"], link, true) == 0))
                    {
                        // already exists
                        return;
                    }
                }
            }
            webpage.Header.Controls.Add(headLink);
        }    
        #endregion

        #region RSS Link
        public static void AddRssFeedLink(Page webpage, string title, string link)
        {
            if (!IsValid(webpage))
                return;            
            HtmlLink feedLink = new HtmlLink();
            feedLink.Attributes["type"] = "application/rss+xml";
            feedLink.Attributes["rel"] = "alternate";
            feedLink.Attributes["title"] = title;
            feedLink.Attributes["href"] = link;
            foreach (Control control in webpage.Header.Controls)
            {
                if (control is HtmlLink)
                {
                    HtmlLink temp = (HtmlLink)control;
                    if ((string.Compare(temp.Attributes["type"], "application/rss+xml", true) == 0) &&
                        (string.Compare(temp.Attributes["title"], title, true) == 0) &&
                        (string.Compare(temp.Attributes["href"], link, true) == 0))
                    {
                        //already exists
                        return;
                    }
                }
            }
            webpage.Header.Controls.Add(feedLink);
        } 
        #endregion  

        #region CSS Links
        public static void AddCssLink(Page webpage, string cssFile)
        {
            if (!IsValid(webpage))
                return;
            webpage.Header.Controls.Add(CreateCssLink(cssFile));
        }

        public static void AddCssLink(Page webpage, string cssFile, bool AddAtTop)
        {
            if (!IsValid(webpage))
                return;
            if (AddAtTop)
                webpage.Header.Controls.AddAt(0, CreateCssLink(cssFile));
            else
                webpage.Header.Controls.Add(CreateCssLink(cssFile));
        }

        public static void AddAdminThemeAwareCssLink(Page webpage, string css)
        {
            string cssFile = WebRoots.GetAdminRoot + "Themes/" + SiteSettings.AdminTheme + "/" + css;
            string CommonCssFile = WebRoots.GetAdminRoot + "Themes/_Common/" + css;
            if (File.Exists(HostingEnvironment.MapPath(cssFile)))
                WebPageController.AddCssLink(webpage, cssFile, false);
            else
                WebPageController.AddCssLink(webpage, CommonCssFile, false);
        }

        private static HtmlLink CreateCssLink(string cssfile)
        {
            HtmlLink cssLink = new HtmlLink();
            cssLink.Attributes["href"] = cssfile;
            cssLink.Attributes["rel"] = "stylesheet";
            cssLink.Attributes["type"] = "text/css";
            return cssLink;
        }  
        #endregion

        #region Script Links
        public static void AddScriptLink(Page webpage, string scriptFile)
        {
            if (!IsValid(webpage))
                return;            
            ClientScriptManager cs = webpage.ClientScript;
            if (!cs.IsClientScriptIncludeRegistered(scriptFile))            
                cs.RegisterClientScriptInclude(scriptFile, scriptFile);              
        }
        #endregion        
      
        #region Private Methods
        private static bool IsValid(Page webpage)
        {
            if (webpage == null)
                return false;
            if (webpage.Header == null)
                return false;
            return true;
        } 
        #endregion        
    }
}
